package com.zhoumy.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * @author zhoumy
 * @description 自定义bean
 * @date 2024年6月27日10:15:08
 */
public class CustomBean implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {

    private String name;
    private String address;

    private BeanFactory beanFactory;
    private String beanName;

    public CustomBean() {
        System.out.println("第6步：【构造器】调用CustomBean的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("第9步：【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("第10步：【注入属性】注入属性address");
        this.address = address;
    }

    @Override
    public String toString() {
        return "CustomBean{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    // 通过spring的xml配置文件<bean>的init-method属性指定的初始化方法
    public void xml_init() {
        System.out.println("第16步：【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过spring的xml配置文件<bean>的destroy-method属性指定的初始化方法
    public void xml_destroy() {
        System.out.println("第21步：【destroy-method】调用<bean>的destroy-method属性指定的销毁方法");
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out
                .println("第12步：【执行BeanFactoryAware接口的setBeanFactory方法】setBeanName后调用");
        this.beanFactory = beanFactory;
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String s) {
        System.out.println("第11步：【执行BeanNameAware接口的setBeanName方法】属性注入后调用, 此时s = "+s);
        this.beanName = s;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("第15步：【执行InitializingBean接口的afterPropertiesSet方法】在processBeforeInitialization之后,配置的xml_init之前调用");
    }

    // 这是DiposibleBean接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("第20步：【执行DiposibleBean接口的destroy方法】在processAfterInitialization之后,配置的xml_destroy之前调用");
    }
}